import { formatDate } from '../../utils/util';
import Dialog from '@vant/weapp/dialog/dialog';
import api from '../../api/index';
// pages/transferCollect/transferCollect.ts
const app = getApp();
const { sites } = app.globalData;
const sitePoolObj: any = {};
sites.forEach((site: SiteItem) => {
  sitePoolObj[site.siteName] = site.pools.map((pool) => {
    return pool.poolNo;
  });
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: app.globalData.userInfo.userName,
    defaultDate: Date.now(),
    formatDate: formatDate(new Date()),
    date: new Date().getTime(),
    outPoolNo: '',
    inPoolNo: '',
    show: false,
    transferNum: 0,
    weight: 0,
    loading: false,
    minDate: new Date().getTime() - 30 * 86400000,
    maxDate: new Date().getTime(),
    columns: [] as any[],
    outPickerShow: false,
    inPickerShow: false,
    dialogShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const app = getApp();
    if (!app.globalData.isLogin) {
      wx.redirectTo({
        url: '/pages/login/login',
        success: () => {
          console.log('重定向到登录页成功');
        },
        fail: (err) => {
          console.log('重定向到登录页失败', err);
        }
      });
    }
    this.setData({
      columns: [
        {
          values: Object.keys(sitePoolObj),
        },
        {
          values: sitePoolObj[Object.keys(sitePoolObj)[0]],
        },
      ],
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
  /**
   * 日期选择弹窗显示/关闭/确认
   */
  onCalendarDisplay() {
    this.setData({
      show: true,
    });
  },
  onCalendarClose() {
    this.setData({
      show: false,
    });
  },
  onCalendarConfirm(event: any) {
    this.setData({
      show: false,
      date: event.detail.getTime(),
      formatDate: formatDate(event.detail)
    });
  },
  /**
   * 分出塘号选择弹窗显示/关闭/确认
   */
  onOutPoolPickerDisplay() {
    this.setData({
      outPickerShow: true,
    });
  },
  onOutPoolPickerClose() {
    this.setData({
      outPickerShow: false,
    });
  },
  onOutPoolPickerConfirm(event: any) {
    this.setData({
      outPickerShow: false,
      outPoolNo: event.detail.value[1]
    });
  },
  onOutPickerChange(event: any) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, sitePoolObj[value[0]]);
  },
  /**
   * 分入塘号选择弹窗显示/关闭/确认
   */
  onInPoolPickerDisplay() {
    this.setData({
      inPickerShow: true,
    });
  },
  onInPoolPickerClose() {
    this.setData({
      inPickerShow: false,
    });
  },
  onInPoolPickerConfirm(event: any) {
    this.setData({
      inPickerShow: false,
      inPoolNo: event.detail.value[1]
    });
  },
  onInPickerChange(event: any) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, sitePoolObj[value[0]]);
  },
  /**
   * 对话框确认按钮异步方法
   * */
  beforeClose(action: string) {
    return new Promise((resolve) => {
      if (action === 'confirm') {
        api.reportDivide({
          poolNoIn: this.data.inPoolNo,
          poolNoOut: this.data.outPoolNo,
          date: this.data.date,
          reporter: this.data.userName,
          quantity: this.data.transferNum,
          weight: this.data.weight
        }).then(res => {
          resolve(res);
          if (res.status === 200) {
            this.setData({
              dialogShow: true
            })
          }
        }).catch(err => {
          console.log(err);
        })
      } else {
        // 拦截取消操作
        resolve(true);
      }
    });
  },
  onBtnClick() {
    if (this.data.transferNum === null || this.data.weight === null || this.data.transferNum === undefined || this.data.weight === undefined || !this.data.inPoolNo || !this.data.outPoolNo) {
      return Dialog.alert({
        title: '上报失败',
        message: '有数据内容为空，请检查后再重新上报！',
      }).then(() => {
        // on close
      });
    }
    return Dialog.confirm({
      title: '请再次确认上报信息',
      message: `上报人：${this.data.userName}\n发生日期：${this.data.formatDate}\n分出塘号：${this.data.outPoolNo}\n分入塘号：${this.data.inPoolNo}\n分出量：${this.data.transferNum} 尾\n分出重量：${this.data.weight} kg`,
      beforeClose: this.beforeClose,
    });
  },
  onTransferNumChange(e: any) {
    this.setData({
      transferNum: Number(e.detail)
    })
  },
  onWeightChange(e: any) {
    this.setData({
      weight: Number(e.detail)
    })
  },
  onDialogClose() {
    this.setData({ dialogShow: false });
  },
});
